greedy math *1300

Please click on ads to support us..

Python Code:

from collections import deque, defaultdict, Counter
from heapq import heappush, heappop, heapify
from math import inf
from functools import lru_cache
from itertools import accumulate
from typing import List
import sys
input = sys.stdin.readline

T = int(input())

for _ in range(T):
    N = int(input())

    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    possible = True
    for i in range(N-1):
        if A[i] > B[i]:
            possible = False
            break
        if A[i] == B[i]:
            continue
        if B[i] <= B[i+1] + 1:
            continue
        possible = False
        break

    if possible:
        if A[N-1] > B[N-1]:
            possible = False
        elif A[N-1] == B[N-1] or B[N-1] <= B[0] + 1:
            pass
        else:
            possible = False
    if possible:
        print('yes')
    else:
        print('no')


C++ Code:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
 
int main()
{
    ios_base::sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin>>n;int a[n],b[n];
        for(int i=0;i<n;i++)
        cin>>a[i];
         bool ans=1;
        for(int i=0;i<n;i++)
        {

        cin>>b[i];
        if(a[i]>b[i])
        ans=false;
        }
        if(!ans)
        {
            cout<<"NO"<<"\n";
            continue;
        }
       
        int temp=a[n-1];
        a[n-1]=b[n-1];
        for(int i=n-2;i>=0;i--)
        {
            if(a[i]!=b[i])
            {          
                a[i]=min(b[i],a[1+i]+1);
                if(a[i]!=b[i])
                {
                    ans=false;
                    break;
                }        

            }
        }
        if(ans)
        {
            if(temp!=b[n-1]&&!(a[n-1]-a[0]<=1))
            ans=false;
        }


        cout<<(ans?"YES":"NO")<<"\n";

    }
}


Comments

Submit
0 Comments
More Questions

318A - Even Odds
550B - Preparing Olympiad
939B - Hamster Farm
732A - Buy a Shovel
1220C - Substring Game in the Lesson
452A - Eevee
1647B - Madoka and the Elegant Gift
1408A - Circle Coloring
766B - Mahmoud and a Triangle
1618C - Paint the Array
469A - I Wanna Be the Guy
1294A - Collecting Coins
1227A - Math Problem
349A - Cinema Line
47A - Triangular numbers
1516B - AGAGA XOOORRR
1515A - Phoenix and Gold
1515B - Phoenix and Puzzle
155A - I_love_username
49A - Sleuth
1541A - Pretty Permutations
1632C - Strange Test
673A - Bear and Game
276A - Lunch Rush
1205A - Almost Equal
1020B - Badge
1353A - Most Unstable Array
770A - New Password
1646B - Quality vs Quantity
80A - Panoramix's Prediction